<template>
  <div>
    <!--标题--Start-->
    <div>
      <div class="base_header" style="text-align: left;margin-bottom: 20px">
        <span style="font-size: 30px;font-weight: normal;">
          角色管理
        </span>
        <span style="font-size: 10px">用户角色管理</span>
        <el-button type="success"
                   style="float: right"
                   @click="handleAdd"
        >新增</el-button>
      </div>
    </div>
    <!--标题--End-->
    <div>
      <el-table
        :data="roleList"
        stripe
        style="width: 100%">
          <el-table-column
            label="序号"
            width="80">
            <template slot-scope="scope">
            <span>
              {{scope.$index + 1 +  (page - 1) * pageSize}}
            </span>
            </template>
          </el-table-column>
          <el-table-column
            label="名称">
            <template slot-scope="scope">
              <span >
              {{scope.row.nickname}}
            </span>
            </template>
          </el-table-column>
          <el-table-column
          label="备注">
            <template slot-scope="scope">
                <span >
                {{scope.row.remark}}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            label="是否激活">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.active"
                @change="updateRoleActive( scope.row)"
                :active-value="1"
                :inactive-value="0"
                active-color="#13ce66"
                inactive-color="#ff4949">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作">
          <template slot-scope="scope">
              <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <div style="display: inline-block" v-if="scope.row.id!=18&&scope.row.id!=19&&scope.row.id!=21&&scope.row.id!=22">
              <el-popconfirm
                title="确定删除吗？"
                @onConfirm="handleDelete(scope.$index, scope.row)"
              >
                <el-button
                  size="mini"
                  slot="reference"
                  type="danger"
                >删除</el-button>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-drawer
      style="margin-top: 30px"
      :title="title"
      :visible.sync="dialogVisible"
      :with-header="true">
      <div style="padding:0px 20px">
        <el-form v-model="roleObj" label-position="left" label-width="100px">
          <el-form-item label="名称">
            <el-input v-model="roleObj.nickname"></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="roleObj.remark"></el-input>
          </el-form-item>
          <el-form-item label="是否激活">
            <el-switch
              v-model="roleObj.active"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </el-form-item>
          <el-form-item label="后台导航">
            <el-tree :data="backMenu"
                     :props="defaultProps"
                     node-key="id"
                     ref="backMenuTree"
                     show-checkbox
            ></el-tree>
          </el-form-item>
          <el-form-item label="管理导航">
            <el-tree :data="manageMenu"
                     :props="defaultProps"
                     node-key="id"
                     ref="manageMenuTree"
                     show-checkbox
            ></el-tree>
          </el-form-item>
        </el-form>
        <div style="text-align: right">
          <el-button type="success"  @click="saveRole(type)">保存</el-button>
        </div>
      </div>
    </el-drawer>
    <!--分页Start-->
    <div class="pagination" style="text-align: center">
      <el-pagination
        @current-change="handleCurrentChange"
        :page-size="pageSize"
        :hide-on-single-page="true"
        layout="prev, pager, next"
        :total="total">
      </el-pagination>
    </div>
    <!--分页End-->
  </div>
</template>

<script>
export default {
  name: 'RoleManage',
  data () {
    return {
      dialogVisible: false,
      roleList: [],
      activeList: [],
      showEdit: [],
      total: 0,
      page: 1,
      pageSize: 10,
      title: '新增角色',
      // 0新增 1编辑
      type: 0,
      backMenu: [],
      manageMenu: [],
      roleObj: {
        nickname: '',
        remark: '',
        active: 1
      },
      nowBackListKey: [],
      nowManageListKey: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  mounted () {
    this.getSize()
    this.getListRole(this.page, this.pageSize)
    this.getMenu(3)
    this.getMenu(4)
  },
  methods: {
    // 统计数量
    getSize () {
      var _this = this
      this.$axios.post('/api/role/getSizeRole')
        .then(function (response) {
          var result = response.data
          if (result.isSuccess) {
            _this.total = parseInt(result.result)
          }
        })
    },
    getListRole (page, pageSize) {
      var _this = this
      var queryData = {
        page: page,
        pageSize: pageSize
      }
      this.$axios.post('/api/role/getListRole',
        this.$qs.stringify({
          queryData: JSON.stringify(queryData)
        }))
        .then(function (response) {
          var result = response.data
          if (result.isSuccess) {
            _this.roleList = result.result
            for (var i = 0; i < _this.roleList.length; i++) {
              _this.activeList.push(_this.roleList[i].active)
            }
          }
        })
    },
    // 下方分页点击
    handleCurrentChange (page) {
      this.page = page
      this.getListRole(page, this.pageSize)
    },
    handleAdd () {
      this.title = '新增角色'
      this.roleObj = {}
      this.type = 0
      this.dialogVisible = true
      this.$refs.backMenuTree.setCheckedKeys([])
      this.$refs.manageMenuTree.setCheckedKeys([])
    },
    handleEdit (index, row) {
      this.title = '编辑角色'
      this.roleObj = JSON.parse(JSON.stringify(row))
      this.type = 1
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.backMenuTree.setCheckedKeys([])
        this.$refs.manageMenuTree.setCheckedKeys([])
      })
      this.getMenuByRoleId(row.id)
    },
    handleDelete (index, row) {
      var _this = this
      var roleData = JSON.parse(JSON.stringify(row))
      roleData.remove = 0
      this.$axios.post('/api/role/updateRole',
        this.$qs.stringify({
          roleData: JSON.stringify(roleData)
        })
      )
        .then(function (response) {
          var data = response.data
          if (data.isSuccess) {
            _this.$message({
              type: 'success',
              message: '删除成功',
              offset: 50
            })
            _this.getListRole(_this.page, _this.pageSize)
          } else {
            _this.$message({
              type: 'success',
              message: '失败，请重试',
              offset: 50
            })
          }
        })
    },
    getMenu (groupId) {
      var _this = this
      var queryData = {}
      queryData.groupId = groupId
      this.$axios.post('/api/path/getAllMenu',
        this.$qs.stringify({
          queryData: JSON.stringify(queryData)
        })
      ).then(function (response) {
        if (response.data.isSuccess) {
          if (groupId === 3) {
            _this.backMenu = response.data.result
          } else if (groupId === 4) {
            _this.manageMenu = response.data.result
          }
        }
      })
    },
    saveRole (type) {
      var _this = this
      // 勾选的key
      var checkedKeys = this.$refs.backMenuTree.getCheckedKeys()
      // 暂选状态的母tab的key
      var halfKeys = this.$refs.backMenuTree.getHalfCheckedKeys()
      // 合并两个数组
      // 勾选的key
      var checkedKeys2 = this.$refs.manageMenuTree.getCheckedKeys()
      // 暂选状态的母tab的key
      var halfKeys2 = this.$refs.manageMenuTree.getHalfCheckedKeys()
      // 合并两个数组
      var url = ''
      if (type === 1) {
        url = '/api/role/updateRole'
      } else if (type === 0) {
        url = '/api/role/add'
      }
      this.$axios.post(url,
        this.$qs.stringify({
          roleData: JSON.stringify(this.roleObj),
          backPaths: checkedKeys.concat(halfKeys).toString(),
          managePaths: checkedKeys2.concat(halfKeys2).toString()
        })
      ).then(function (response) {
        var data = response.data
        if (data.isSuccess) {
          _this.$message({
            type: 'success',
            offset: 50,
            message: '成功'
          })
          _this.dialogVisible = false
          _this.getListRole(_this.page, _this.pageSize)
        } else {
          _this.$message({
            type: 'error',
            offset: 50,
            message: '失败，请重试'
          })
        }
      })
    },
    updateRoleActive (row) {
      var _this = this
      var roleData = JSON.parse(JSON.stringify(row))
      this.$axios.post('/api/role/updateRole',
        this.$qs.stringify({
          roleData: JSON.stringify(roleData)
        })
      )
        .then(function (response) {
          var data = response.data
          if (data.isSuccess) {
            _this.$message({
              type: 'success',
              message: '修改成功',
              offset: 50
            })
            _this.getListRole(_this.page, _this.pageSize)
          } else {
            _this.$message({
              type: 'success',
              message: '失败，请重试',
              offset: 50
            })
          }
        })
    },
    getMenuByRoleId (roleId) {
      this.nowBackListKey = []
      this.nowManageListKey = []
      var _this = this
      this.$axios.post('/api/menu/getListMenu',
        this.$qs.stringify({
          roleId: roleId
        })
      ).then(function (response) {
        var data = response.data
        if (data.isSuccess) {
          for (var i = 0; i < data.result.length; i++) {
            var re = data.result[i]
            if (re.type === 3) {
              _this.nowBackListKey = re.pathIds
            } else if (re.type === 4) {
              // if (re.pathIds.length>0)
              // _this.nowManageListKey = re.pathIds
              for (var jj = 0; jj < re.pathIds.length; jj++) {
                let now = {}
                now.id = re.pathIds[jj]
                _this.nowManageListKey.push(now)
              }
            }
          }
          _this.$refs.backMenuTree.setCheckedKeys(_this.nowBackListKey)
          _this.$refs.manageMenuTree.setCheckedNodes(_this.nowManageListKey)
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
